#Perl Script for creation of RDF  triples (Author)  and (Paper)  and (Media)from tab separated file of records.
#Author : Bhargava Deepak Konidena 
# Under the guidance of Prof. Ying Ding.


#LWP UserAgent is required for creating ua request and accessing the content.
use LWP::UserAgent;
use HTTP::Request::Common qw(GET);
use WWW::Mechanize;

#Declarations of the anonymous Hashes and Arrays
my %table;
my %Author;
my %Email;
my %Citation;
my %DocumentID;
my %Title;
my %DatePublished;
my %Descriptors;
my %ISSN;
my %Volume;
my %Issue;
my %StartPage;
my %EndPage;
my %DCAuthor;
my %BiboAuthorList;
my %PublicationType;
my %PublicationSource;
my %PublicationSourceShortName;
my %Publisher;

my @AuthorList;
my @EmailList;
my @CitationList;
my @DescriptorList;
my @AuthorName;
my @WebsiteName;

#Declarations of Scalar Variables
my $AuthorCount = 0;
my $EmailCount = 0;
my $CitationCount = 0;
my $DescriptorCount = 0;
my $AuthorFamilyName = "";
my $AuthorGivenName = "";
my $Website = "";
my $AuthorDBLPPage = "";
my $AuthorHomePage = "";
my $CoAuthorDBLPPage = "";
my $PublisherWebPage = "";
my $count = 0 ;


#example_bibo.txt is the downsized file . It contains only three columns (AF, EM and DI)
open(BIBO,"example_bibo_media.txt") or die "cant open BIBO: $!";
#bibo_dsz_test5 is the RDF file generated
open(BIBO_DSZ,">bibo_dsz_test9") or die "cant open BIBO_DSZ: $!";

while(<BIBO>){
	chomp;
	my ($PT, $AF, $TI, $SO, $DE, $EM, $CR, $PU, $SN, $DI, $J9, $PY, $VL, $IS, $BP, $EP, $ISI) = split /\t+/;
	push @{$table{$count}} , $AF, $EM, $CR; 
	$PublicationType{$count} = $PT;
	$PublicationSource{$count} = $SO;
	$Publisher{$count} = $PU;
	$PublicationSourceShortName{$count} = $J9;
	$Author{$count} = $AF;
	$Email{$count} = $EM;
	$Citation{$count} = $CR;
	$Title{$count} = $TI;
	$Descriptors{$count} = $DE;
	$ISSN{$count} = $SN;
	$DocumentID{$count} = $DI;
	$DatePublished{$count} = $PY;
	$Volume{$count} = $VL;
	$Issue{$count} = $IS;
	$StartPage{$count} = $BP;
	$EndPage{$count} = $EP;
	
	
	$count++;
}
#count is required to keep a count of the no.of lines/records in the original file
$count = 0 ;
foreach $count (keys %table){
	
		# Separating Author list and Email List and Citation List using delimiters
		@AuthorList = split(';',$Author{$count});
		print "@AuthorList\n";
		@EmailList = split(';',$Email{$count});
		print "@EmailList\n";
		@CitationList = split(';',$Citation{$count});
		print "@CitationList\n";
		@DescriptorList = split(';',$Descriptors{$count});
		print "@DescriptorList\n";
		
		
		#Lengths of the respective arrays
		$AuthorCount = @AuthorList;
		print "$AuthorCount\n";
		$EmailCount = @EmailList;
		print "$EmailCount\n";
		$CitationCount = @CitationList;
		print "$CitationCount\n";
		$DescriptorCount = @DescriptorList;
		print "$DescriptorCount\n";
		
		
		#This is for the records whose number of mail addresses matches the number of Authors/Co-authors. Not all records have these numbers same.
				for(my $i = 0; $i < $AuthorCount; $i++) {
				print "$i\n";
				# Splitting the FamilyName and GivenName of each author using delimiter ','
				@AuthorName = split(',', $AuthorList[$i]);
				#The first field after splitting contains the FamilyName and the next, GivenName
								
				$AuthorFamilyName = $AuthorName[0];
				$AuthorGivenName = $AuthorName[1];
				
				# Removing the quotes if any present in the names.
				 $AuthorFamilyName =~ s/("|")//g;
				 $AuthorGivenName =~ s/("|")//g;
				
				#Removing leading and trailing spaces from $AuthorFamilyName and $AuthorGivenName
				$AuthorFamilyName =~ s/(^\s+|\s+$)//;
				#$AuthorFamilyName =~ s/\s+$//;
				$AuthorGivenName =~ s/(^\s+|\s+$)//;
				#$AuthorGivenName =~ s/\s+$//;
				
				#For dc:contributor, the mail address is stripped and http://www is appended to the mail domain.
				if ($AuthorCount == $EmailCount) {
				@WebsiteName = split('@', $EmailList[$i]);
				}
				elsif ($AuthorCount != $EmailCount) {
				@WebsiteName = split('@', $EmailList[0]);
				}
				$Website = $WebsiteName[1];
				
				#user agent creation and GET request for Author's Homepage
				my $ua = LWP::UserAgent->new;
    			$ua->agent('Mozilla/8.0');
				my  $req = HTTP::Request->new(GET => "http://www.google.com/search?hl=en&q=$AuthorList[$i]&btnI=I\'m\+Feeling\+Lucky&aq=f&oq=&aqi=");
				my $res = $ua->request($req);
				$AuthorHomePage =  $res->base;;
				
				#user agent creation and GET request for Author's DBLP page <unique Identifier>
				my $ua1 = LWP::UserAgent->new;
    			$ua1->agent('Mozilla/8.0');
				my  $req1 = HTTP::Request->new(GET => "http://www.google.com/search?hl=en&q=DBLP\+$AuthorList[$i]&btnI=I\'m\+Feeling\+Lucky&aq=f&oq=&aqi=");
				my $res1 = $ua1->request($req1);
				$AuthorDBLPPage =  $res1->base;
				
				#Copying $AuthorDBLPPage to $DCAuthor{$count}
				$DCAuthor{$count} = $AuthorDBLPPage;

				
				#The Actual RDF schema and triple generation
				print BIBO_DSZ "#Record $count \n";
				print BIBO_DSZ "<$AuthorDBLPPage>";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "a foaf:Person ;";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "foaf:givenname $AuthorGivenName ;";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "foaf:family_name $AuthorFamilyName ;";
				print BIBO_DSZ "\n";
				if ($AuthorCount == $EmailCount) {
					print BIBO_DSZ "foaf:mbox <mailto:$EmailList[$i]> ;";
				}
				elsif ($AuthorCount != $EmailCount) {
					print BIBO_DSZ "foaf:mbox <mailto:$EmailList[0]> ;";
				}
				print BIBO_DSZ "\n";
				print BIBO_DSZ "dc:contributor <http://www.$Website> ;";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "foaf:homepage <$AuthorHomePage> ;" ;
				print BIBO_DSZ "\n";
				if ( $i != 0) {
					print BIBO_DSZ "\n\n\n\n";
				}
				
				if ( $i == 0) {
				# This for loop is for the Number of Co-Authors for each author.
				#my is very important. It defines the scope for $i.
					for( my $i = 1; $i < $AuthorCount; $i++) {
						my $ua1 = LWP::UserAgent->new;
						$ua1->agent('Mozilla/8.0');
						my  $req1 = HTTP::Request->new(GET => "http://www.google.com/search?hl=en&q=DBLP\+$AuthorList[$i]&btnI=I\'m\+Feeling\+Lucky&aq=f&oq=&aqi=");
						my $res1 = $ua1->request($req1);
						$CoAuthorDBLPPage  = $res1->base;
						#copying $CoAuthorDBLPPage to $BiboAuthorList{
						$BiboAuthorList{$count} .=" <$CoAuthorDBLPPage>";
						print BIBO_DSZ "foaf:knows <$CoAuthorDBLPPage> ." ;
						print BIBO_DSZ "\n";
					}
					print BIBO_DSZ "\n\n\n\n";
				}
		}
		
		# This is for printing the paper associated with the record
				print BIBO_DSZ "#Paper $count\n";
				print BIBO_DSZ "<doi:$DocumentID{$count}> a bibo:Article ;";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "dc:title $Title{$count} ;";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "dc:date $DatePublished{$count} ;";
				print BIBO_DSZ "\n";
				
				#This is for looping through the subject descriptors and printing them separately.
					for(my $i = 0; $i < $DescriptorCount ; $i++) {
						print BIBO_DSZ "dc:subject $DescriptorList[$i] ;";
						print BIBO_DSZ "\n";
				}
				
				print BIBO_DSZ "dc:isPartOf <urn:issn:$ISSN{$count}> ;";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "dc:volume $Volume{$count} ;";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "dc:issue $Issue{$count} ;";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "dc:pageStart $StartPage{$count} ;";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "dc:pageEnd $EndPage{$count} ;";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "dc:creator <$DCAuthor{$count}> ;";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "bibo:authorList ($BiboAuthorList{$count}) .";
				print BIBO_DSZ "\n\n\n\n";
							
			
				#This section is for printing RDF triples for media related to the record
				#user agent creation and GET request for Publisher's Webpage 
				my $ua2 = LWP::UserAgent->new;
    			$ua2->agent('Mozilla/8.0');
				my  $req2 = HTTP::Request->new(GET => "http://www.google.com/search?hl=en&q=$Publisher{$count}&btnI=I\'m\+Feeling\+Lucky&aq=f&oq=&aqi=");
				my $res2 = $ua2->request($req2);
				$PublisherWebPage =  $res2->base;
				
				#The Actual RDF schema and triple generation
				print BIBO_DSZ "#Media $count \n";
				if ( $PublicationType{$count} eq  "J" ){
					print BIBO_DSZ "<urn:issn: $ISSN{$count}> a bibo:Journal ;";
				}
				elsif( $PublicationType{$count} eq "C"){
					print BIBO_DSZ "<urn:issn: $ISSN{$count}> a bibo:Conference ;";
				}
				print BIBO_DSZ "\n";
				print BIBO_DSZ "dc:title $PublicationSource{$count} ;";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "dc:publisher <$PublisherWebPage> ;";
				print BIBO_DSZ "\n";
				print BIBO_DSZ "bibo:shortTitle $PublicationSourceShortName{$count} ;";
				print BIBO_DSZ "\n";
				
				
				
		print BIBO_DSZ "\n\n\n\n";
				
}

close(BIBO);
close(BIBO_DSZ);
  